#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>

bool minimize(int&a,int b){if(a<=b)return 0;a=b;return 1;}

char maze[400];
int dir[4][2] = {-1,0,0,-1,1,0,0,1}, rd[4]={0,3,2,1};
int dp[150][150],inq[150][150],H;
const int INF = 0x3f3f3f3f;

int walk(int p,int d){
	int x=p/15+dir[d][0],y=p%15+dir[d][1];
	if(x<0||x>9||y<0||y>14||maze[x*15+y]=='X')return p;
	else return x*15+y;
}

bool neighbor(int p1,int p2){
	return abs(p1/15-p2/15)+abs(p1%15-p2%15)==1;
}

int solve(int p1,int p2){
	memset(dp, 0x3f, sizeof(dp));
	memset(inq, 0, sizeof(inq));
	dp[p1][p2]=0;
	std::queue<int> Q1, Q2;
	Q1.push(p1);Q2.push(p2);
	while(!Q1.empty()){
		p1=Q1.front();p2=Q2.front();
		Q1.pop();Q2.pop();
		inq[p1][p2]=false;
		if(maze[p1]=='O'&&maze[p2]=='O')continue;
		if(maze[p1]=='O'){
			if(neighbor(p1,p2)){
				if(minimize(dp[p2][p2],dp[p1][p2]+11)&&!inq[p2][p2]){
					Q1.push(p2);Q2.push(p2);inq[p2][p2]=1;
				}
			}
			for(int d=0;d<4;++d){
				int np=walk(p2,d);
				if(minimize(dp[p1][np],dp[p1][p2]+3)&&!inq[p1][np]){
					Q1.push(p1);Q2.push(np);inq[p1][np]=1;
				}
			}
		}
		else if(maze[p2]=='O'){
			if(neighbor(p1,p2)){
				if(minimize(dp[p1][p1],dp[p1][p2]+11)&&!inq[p1][p1]){
					Q1.push(p1);Q2.push(p1);inq[p1][p1]=1;
				}
			}
			for(int d=0;d<4;++d){
				int np=walk(p1,d);
				if(minimize(dp[np][p2],dp[p1][p2]+2)&&!inq[np][p2]){
					Q1.push(np);Q2.push(p2);inq[np][p2]=1;
				}
			}
		}
		else{
			for(int d=0;d<4;++d){
				int np1=walk(p1,d),np2=walk(p2,rd[d]);
				if(minimize(dp[np1][np2],dp[p1][p2]+5)&&!inq[np1][np2]){
					Q1.push(np1);Q2.push(np2);inq[np1][np2]=1;
				}
			}
		}
	}
	return dp[H][H];
}

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		int s1, e1, s2, e2;
		for (int i = 0; i < 150; i++) {
			std::cin >> maze[i];
			if (maze[i] == 'H' ) H = i;
		}
		std::cin >> s1 >> e1 >> s2 >> e2;
		int p1=s1*15+e1-16,p2=s2*15+e2-16;
		int ans = std::min(solve(p1,p2),solve(p2,p1));
		if (ans == INF) ans = -1;
		std::cout << ans << '\n';
	}
}

